package com.bwie.auth.controller;

import com.bwie.auth.service.AuthService;
import com.bwie.common.domain.SysUser;
import com.bwie.common.domain.request.LoginReq;
import com.bwie.common.domain.response.JwtRes;
import com.bwie.common.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@Log4j2
public class TokenController {
    @Autowired
    private AuthService authService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 登录
     * @param loginReq
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody @Valid LoginReq loginReq){
        log.info("功能名称：登录，请求url：{}，请求方式：{}，请求参数：{}",request.getRequestURI(),request.getMethod(),loginReq);
        JwtRes jwtRes=authService.login(loginReq);
        Result<JwtRes> result = Result.success(jwtRes);
        log.info("功能名称：登录，请求url：{}，请求方式：{}，响应结果：{}",request.getRequestURI(),request.getMethod(),result);
        return result;
    }

    /**
     * 获取登录用户信息
     * @return
     */
    @GetMapping("userInfo")
    public Result userInfo(){
        log.info("功能名称：获取登录用户信息，请求url：{}，请求方式：{}",request.getRequestURI(),request.getMethod());
        SysUser sysUser = authService.userInfo();
        Result<SysUser> result = Result.success(sysUser);
        log.info("功能名称：获取登录用户信息，请求url：{}，请求方式：{}，响应结果：{}",request.getRequestURI(),request.getMethod(),result);
        return result;
    }
}
